package fr.alma.domo.music;

import fr.alma.domo.model.CollaborationSchema;
import fr.alma.domo.model.Description;
import fr.alma.domo.music.rc.HP;
import fr.alma.domo.music.rc.Player;

public class PlayerHPCollaboration implements CollaborationSchema {

	public final static String PLAYER_SERVICE_NAME = "player";
	public final static String HP_SERVICE_NAME = "hp";
	
	@Service(name=PLAYER_SERVICE_NAME) Player player;	
	@Service(name=HP_SERVICE_NAME) HP hp;
	
	private String playing = null;
	
	@Override
	public Description getDescriptionFor(String name) {
		Description d = new Description();
		if(HP_SERVICE_NAME.equals(name)){
			d.addScope(HP.RESOURCE_FAMILY);
			d.addProperty(HP.INSTANCE_OF, HP.class);
			d.addProperty(HP.BUSY, false);
			if(hp!=null){
				String pos = hp.getAttribute(HP.SOUND_TRANSFER_NEED, String.class);
				d.addProperty(HP.POSITION, pos==null || PositionService.NO_POSITION.equals(pos)?hp.getPosition():pos);
			}
		}
		else if(PLAYER_SERVICE_NAME.equals(name)){
			d.addScope(Player.RESOURCE_FAMILY);
			d.addProperty(Player.INSTANCE_OF, Player.class);
			d.addProperty(Player.PLAYING_TITLE, null);
			if(player!=null){
				playing = player.getPlayingTitle();
				d.addSequenceProperty(Player.SONG_LIST, player.getPlayingTitle());
			}
		}
		return d;
	}

	@Override
	public void negociate() {
		hp.setBusy(true);
	}

	@Override
	public void renegociate() {
		hp.disableNotification();
		hp.setBusy(true);
		hp.setAttribute(HP.SOUND_TRANSFER_NEED, PositionService.NO_POSITION);
		hp.enableNotification();
		if(playing!=null){
			player.setPlayingTitle(playing);
		}
	}

}
